<?php
    namespace Cheukpang\Mq;
    
    class HttpProducer
    {
        //签名
        private static $signature = "Signature";
        //在MQ控制台创建的Producer ID
        private static $producerid = "ProducerID";
        //阿里云身份验证码
        private static $aks = "AccessKey";
        //配置信息
        private static $configs = null;
        
        /**
         * HttpProducer constructor.
         *
         * @param string $config 配置文件路径
         */
        public function __construct($config)
        {
            //读取配置信息
            self::$configs = parse_ini_file($config);
        }
        
        /**
         * 发布消息流程
         */
        public function process()
        {
            //打印配置信息
//            var_dump(self::$configs);
            //获取Topic
            $topic = self::$configs["Topic"];
            //获取保存Topic的URL路径
            $url = self::$configs["URL"];
            //读取阿里云访问码
            $ak = self::$configs["Ak"];
            //读取阿里云密钥
            $sk = self::$configs["Sk"];
            //读取Producer ID
            $pid = self::$configs["ProducerID"];
            //HTTP请求体内容
            $body    = utf8_encode("阿里巴巴");
            $newline = "\n";
            //构造工具对象
            for ($i = 0; $i < 500; $i++) {
                //计算时间戳
                $date = time() * 1000;
                //POST请求url
                $postUrl = $url."/message/?topic=".$topic."&time=".$date."&tag=http&key=http";
                //签名字符串
                $signString = $topic.$newline.$pid.$newline.$this->md5($body).$newline.$date;
                //计算签名
                $sign = Util::calSignature($signString, $sk);
                //初始化网络通信模块
                $ch = curl_init();
                //构造签名标记
                $signFlag = self::$signature.":".$sign;
                //构造密钥标记
                $akFlag = self::$aks.":".$ak;
                //标记
                $producerFlag = self::$producerid.":".$pid;
                //构造HTTP请求头部内容类型标记
                $contentFlag = "Content-Type:text/html;charset=UTF-8";
                //构造HTTP请求头部
                $headers = [
                    $signFlag,
                    $akFlag,
                    $producerFlag,
                    $contentFlag,
                ];
                //设置HTTP头部内容
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                //设置HTTP请求类型,此处为POST
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
                //设置HTTP请求的URL
                curl_setopt($ch, CURLOPT_URL, $postUrl);
                //设置HTTP请求的body
                curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
                //构造执行环境
                ob_start();
                //开始发送HTTP请求
                curl_exec($ch);
                //获取请求应答消息
                $result = ob_get_contents();
                //清理执行环境
                ob_end_clean();
                //打印请求应答结果
                var_dump($result);
                //关闭连接
                curl_close($ch);
            }
        }
        
        /**
         * 计算md5
         *
         * @param $str
         *
         * @return string
         */
        private function md5($str)
        {
            return md5($str);
        }
    }
    